/**
 * Copyright (c) 2009 Teoti Graphix, LLC See:
 *     http://tkarchitecture.teotigraphix.org
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 * @author Michael Schmalle mschmalle@teotigraphix.com
 */
package test.suite.dxframework.core
{

import mx.events.FlexEvent;
import mx.events.PropertyChangeEvent;

import org.flexunit.Assert;
import org.flexunit.async.Async;
import org.fluint.uiImpersonation.UIImpersonator;
import org.teotigraphix.dx.core.ToolKitUtil;

import test.suite.dxframework.support.behavior.TestBehavior;
import test.suite.dxframework.support.component.TestComponent;

public class TC_ToolKitUtil
{
	protected var element:TestComponent;
	protected var behavior:TestBehavior;
	
	[Before(ui,async)]
	public function setUp():void
	{
		element = new TestComponent();
		behavior = new TestBehavior();
		
		Async.proceedOnEvent(this, element, FlexEvent.CREATION_COMPLETE);
		UIImpersonator.addChild( element );
	}
	
	[After]
	public function tearDown():void
	{
		UIImpersonator.removeChild( element );
		element = null;
		behavior = null;
	}
	
	//--------------------------------------------------------------------------
	//
	//  Public Test :: Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	//  utility
	//----------------------------------
	
	[Test(async)]
	/**
	 * Tests the <code>dispatchChangeEvent()</code> method.
	 */
	public function test_call_dispatchChangeEvent():void
	{
		var o:Object = {};
		o.element = element;
		o.property = "visible";
		o.type = o.property + "Changed";
		o.oldValue = false;
		o.newValue = true;
		
		Async.handleEvent( this, element, o.type, 
			element_call_dispatchChangeEvent_Handler, 500, o );
		
		ToolKitUtil.dispatchChangeEvent(
			element, o.property, o.oldValue, o.newValue );
	}
	
	[Test]
	/**
	 * Tests the <code>getClass()</code> method.
	 */
	public function test_call_getClass():void
	{
		var clazz:Class = ToolKitUtil.getClass( element );
		Assert.assertStrictlyEquals( "", clazz, TestComponent );
	}
	
	[Test]
	/**
	 * Tests the <code>getReflection()</code> method.
	 */
	public function test_call_getReflection():void
	{
		var reflection:XML = ToolKitUtil.getReflection( element );
		Assert.assertEquals( "", 
			"test.suite.dxframework.support.component::TestComponent", 
			reflection.@name );
//		Assert.assertEquals( "", 
//			"org.teotigraphix.dx.patterns.component::DXComponent", 
//			reflection.@base );
	}
	
	[Test]
	/**
	 * Tests the <code>getClassName()</code> method.
	 */
	public function test_call_getClassName():void
	{
		var name:String = ToolKitUtil.getClassName( element );
		Assert.assertEquals( "", "test.suite.dxframework.support.component.TestComponent", name );
		name = ToolKitUtil.getClassName( element, false );
		Assert.assertEquals( "", "TestComponent", name );
		name = ToolKitUtil.getClassName( element, true, false );
		Assert.assertEquals( "", "test.suite.dxframework.support.component::TestComponent", name );
	}
	
	[Test]
	/**
	 * Tests the <code>getDefinition()</code> method.
	 */
	public function test_call_getDefinition():void
	{
		var name:String = ToolKitUtil.getClassName( element );
		var clazz:Class = ToolKitUtil.getDefinition( name ) as Class;
		Assert.assertStrictlyEquals( "", clazz, TestComponent );
	}

	/**
	 * Tests the <code>getSingleton()</code> method.
	 */
//	public function test_call_getSingleton():void
//	{
//	}
	
	//--------------------------------------------------------------------------
	//
	//  Protected :: Handlers
	//
	//--------------------------------------------------------------------------
	
	/**
	 * @private
	 */
	protected function element_call_dispatchChangeEvent_Handler( event:PropertyChangeEvent, data:Object ):void 
	{
		Assert.assertEquals( "event.type", data.type, event.type );
		Assert.assertEquals( "event.property", data.property, event.property );
		Assert.assertEquals( "event.oldValue", data.oldValue, event.oldValue );
		Assert.assertEquals( "event.newValue", data.newValue, event.newValue );
		Assert.assertStrictlyEquals( "", data.element, event.currentTarget );		
	}
}
}